from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from time import sleep

url = 'https://www.bilibili.com/'
chrome_options = Options()
chrome_options.add_argument("--window-size=3840,2160")
prefs = {'profile.managed_default_content_settings.images': 2}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options=chrome_options,executable_path='/usr/local/bin/chromedriver')
driver.get(url)

feed_card = driver.find_elements(By.XPATH, './/div[@class="feed-card"]')

for card in feed_card:
    item = {}
    item['title'] = card.find_element(By.XPATH, './/h3[@class="bili-video-card__info--tit"]').get_attribute('title')
    item['link'] = card.find_element(By.XPATH, './/h3[@class="bili-video-card__info--tit"]/a').get_attribute(
        'data-target-url')
    # item['videoid'] = item['link'].split('/')[-1]
    item['play_time'] = card.find_element(By.XPATH, './/div[@class="bili-video-card__stats--left"]/span[1]/span').text
    item['comments'] = card.find_element(By.XPATH, './/div[@class="bili-video-card__stats--left"]/span[2]/span').text
    item['duration'] = card.find_element(By.XPATH, './/span[@class="bili-video-card__stats__duration"]').text
    item['author'] = card.find_element(By.XPATH, './/span[@class="bili-video-card__info--author"]').get_attribute('title')
    item['date'] = card.find_element(By.XPATH, './/span[@class="bili-video-card__info--date"]').text.replace('· ', '')


    print(item)

sleep(3)

driver.quit()
